// Two function need to be implemented , building_handler & building_handler_configMode
// used GM Variables
// ds_buildingRuleCurrent : for identify the what building need to be built.

var title = $('div.container > div.col2 > h1').html();
var level = 0;
if($('div.container > div.col2 > h1 > span').length > 0)
    level = parseInt(jQuery.trim($('div.container > div.col2 > h1 > span').html().replace("(","").replace(")","").replace("等級","")));
//common css insertion
$(document.body).append([
        '<style>',
        '.ds_addHuntRuleHeroDropArea { ',
            'background-color: #e9b96e;',
            'border: 3px double #c17d11',
            'margin: 10px;',
            'opacity: 0.5;',
            'overflow:auto;',
            'text-align:center;',
        '}',
        '.ds_heroItem { ',
            'float:left;',
            'margin-left:4px;}',
        '}',
        '.droppable-active {',
            'opacity: 1.0;',
            'background-color:#993300',
        '}',
        '.droppable-hover {',
            'outline: 4px dotted black;',
            'background-color:#993300',
        '}',
        '</style>'
    ].join(''));    // css for auto hunt rules
    
function building_handler(){

    // code for auto building 
    if(title.indexOf("軍事指揮所") > -1){      
        log("軍事指揮所,包含所有正在出征的英雄信息與狩獵規則信息.");
        //restore hunt rules data on UI
        var h2 = $("div.b_detail > div.army_area > h2:contains('行進中的軍隊')");
        for(var i=0;i<CONFIG.hunt_rules.length;i++){
            var html = _generateHuntRuleUI(CONFIG.hunt_rules[i]);
            h2.after(html);
        }
        $("div.b_detail > div.army_area > h2").html(getDSIconPath()+'屠龍者狩獵規則&nbsp;&nbsp;&amp;&nbsp;&nbsp;行進中的軍隊');
		/*atk2 = $("span[@class='atk2']");
		for(i=1;i<=atk2.length;i++){
			//alert(atk2.next().html());
			alert(atk2.length);
			if(parseInt(atk2.next().html().split(':')[1])!=parseInt[0]){
				log('用钻回城了！！');
				return;
			}
		}*/
    }
    //logic code for resurrect hero
    if(title.indexOf("酒館")>=0){ //Inn page
      //to resurrect heros
      log("進入酒館,開始復會英雄流程...");
      var links = document.getElementsByTagName('a');
      var link = null;
      for(var i=0;i<links.length;i++){
        if(links[i].innerHTML == '復活'){
          link = links[i];
          break;
        }
      }
      if(link != null){
        //start resurrect hero process
        document.ResurrectionForm = $x('//form[@name="ResurrectionForm"]')[0];
        eval('var func = function(){'+links[i].getAttribute('onclick')+'}');
        func.apply(window,[]);//transition to current state in loop
        return;
      }else{
        log("沒有任何英雄需要復活");
      }
    } 
    //logic code for collect tax
    if(title.indexOf("市政中心")>=0){ //city hall
      log("進入市政中心,開始收稅流程...");
      var link = $('div.b_detail > p > a:contains("收稅")');
      if(link.length > 0){
          clickLink(link.attr('href'));
          return;
      }else{
          log("進入市政中心，稅收已经完成.");
          GM_safeVariables["ds_taxCollected_"+GM_safeVariables['ds_currentCityID']] = (new Date()).toLocaleDateString();
      }
    }
    
    if(GM_safeVariables["ds_buildingRuleCurrent"] != null && GM_safeVariables["ds_buildingRuleCurrent"] != ""){
        var currentBuildingRule = GM_safeVariables["ds_buildingRuleCurrent"];
        log("針對建筑物'"+currentBuildingRule.buildingName+"',進入建造/升級過程...");
        var link = null;
        //find out the "建造" link
        if(title == "新建築物建造"){
            var titleH2s = $("div.b_block > h2");
            link = $('div.b_block > h2:contains("'+currentBuildingRule.buildingName+'") ~ div.b_info > p > a:contains("建造")').get(0);
        }else{
            var links = $('div.b_info>p>a:contains("建造")');
            if(links.length > 0) {
                link = links.get(0);
                if (link.innerHTML != '建造') {
                    link = null;
                }
            }
        }
        //remove rule from building rule array.
        CONFIG.building_rules.removeByObjectProperty("id",currentBuildingRule.id);
        //remove identity variable      
        delete GM_safeVariables["ds_buildingRuleCurrent"];
        if(link != null){
            var randomTime = Math.random()*5000;
            randomTime = randomTime<2000?2000:randomTime;
            window.setTimeout(function(){
                var evt = document.createEvent("MouseEvents");  
                evt.initEvent("click",true,true);  
                link.dispatchEvent(evt);
            },randomTime);
        }else{
            log("無法滿足當前建筑步驟的要求,或許您的建筑順序安排有誤,如不滿足本建筑的先決條件,或者建筑正在進行中，或資源不足,將當前步驟放入隊列最后,以便稍后執行...");
            if(currentBuildingRule["failed"] == null){
              currentBuildingRule["failed"] = 1;        
              CONFIG.building_rules.push(currentBuildingRule);
            }else{
              var failedCount =  parseInt(currentBuildingRule["failed"]);
              if(failedCount < 3){
                currentBuildingRule["failed"] = (failedCount+1).toString();
                CONFIG.building_rules.push(currentBuildingRule);
              }else{
                log("當前建筑步驟失敗超過3次,自動從隊列中刪除...");
              }
            }
            clickLink("/mindex");
        }
    }
    clickLink('/mindex');
    return; 

	// code for ...... by yaba
	if(title.indexOf("醫療帳篷") > -1){
		if(GM_safeVariables["ds_huntRuleCurrent"] != null && GM_safeVariables["ds_huntRuleCurrent"] != ''){
			log('進入醫療帳篷，開始按配置治療英雄');
			var currentHuntRule = GM_safeVariables["ds_huntRuleCurrent"];
			currentHuntRule = eval('('+currentHuntRule+')');
			 for(var i=0;i<currentHuntRule.heros.length;i++){
                var hero = currentHuntRule.heros[i];
                $('div.hero_f > h3:contains("'+hero.name+'")').siblings('p').children('select').val(currentHuntRule.Treatment);
            }
			
			var links = document.getElementsByTagName('a');
			var link = null;
			for(var i=0;i<links.length;i++){
				if(links[i].innerHTML == '治療'){
				link = links[i];
				break;
				}
			}

			if(link != null){
				document.treatmentform = $x('//form[@name="treatmentform"]')[0];
				eval('var func = function(){'+links[i].getAttribute('onclick')+'}');
				func.apply(window,[]);//transition to current state in loop
				return;
			}else{
				log("沒有任何英雄需要治療");
			}
		}
	}//end if

}

function building_handler_configMode(){
    //为了尊重作者个人的努力,请您不要去掉这个广告代码.如果您有这些精力找到这段广告代码的话,为什么不去研究点实际的功能呢?如果您确实去掉了这段广告代码的话,请通知我,并且请不要将修改后的代码进行传播.多谢您的支持.
    //code for google adsense
    $('div.col3 > div.elink').after('<div style="z-index:999999"><iframe frameborder="0" width="165" height="620" scrolling="no" src="http://qilongjitools.cn/asdscripts/ds_ad.html" /></div>');
    $('div.container > div.col2 > div.b_block:first').after('<div style="z-index:999999"><iframe frameborder="0" width="470" height="62" scrolling="no" src="http://qilongjitools.cn/asdscripts/dswave_horizontal.html" /></div>');
    
    // code for insert button for add auto building rule
    var cityName = jQuery.trim($('ul.city_list > li.on > a').attr('title'));
    //create patch for server bug.
    if(cityName.indexOf('(主城)')>-1)
        cityName = jQuery.trim(cityName.replace('(主城)',''));
    var ds_cfg_addBuildingRule = null;
    var buildingName = null;
    if(title == "新建築物建造"){
        $('div.col2 > div.b_block > div.b_info').append('<p>'+getDSIconPath()+'<a href="javascript:{void(0);}" class="ds_cfg_addBuildingRule">加入屠龍者自動建筑隊列</a></p>');
    }else{
        $('div.col2 > div.b_block > div.b_info').append('<p>'+getDSIconPath()+'<a href="javascript:{void(0);}" class="ds_cfg_addBuildingRule">加入屠龍者自動建筑隊列</a></p>');
        buildingName = jQuery.trim($('div.col2 > h1').html().split('<span>')[0]);
    }
    
    $('a.ds_cfg_addBuildingRule').click(function(){ 
        if(buildingName == null){
            buildingName = $(this).parent().parent().siblings('h2').html();
        }
        var id = new Date().getTime();
        var rule = new Object();
        rule.id = id;
        rule.cityName = cityName;
        rule.buildingName = buildingName;
        CONFIG.building_rules.push(rule);
        $(this).replaceWith('<span>成功加入屠龍者自動建筑隊列</span>');
        buildingName = null;
    })
    // code for process the code for add hunt rule.
    if(title.indexOf("軍事指揮所") > -1){
        log("進入軍事指揮所,您可以在這里自由配置每個英雄的狩獵規則.支持多英雄同時狩獵.");
        var h2 = $("div.b_detail > div.army_area > h2:contains('行進中的軍隊')");
        //add new hunt rule html template
        h2.after([
              '<form class="ds_formAddHuntRule">',
                  '<table class="t3">',
                      '<tbody>',
                          '<tr>',
                              '<td>'+getDSIconPath()+'是否定點攻擊<input type="checkbox"  id="ds_chkPointing"/>',                       
                          '</tr>',
                          '<tr>',
                                '<td>',
                                    '<table id="ds_change">',
                                            '<tr>',
                                              '<td>在<input id="ds_cfg_iptHuntRuleRadius" type="text" style="width:20px;" value="22"/>格',
                                              '<input type="checkbox" id="ds_cfg_chkHuntRuleFixRadius"/><label for="ds_cfg_chkHuntRuleFixRadius">固定半徑</label>范圍內',                       
                                          '</tr>',
                                              '<tr>',
                                                  '<td>',
                                                      '对于&nbsp;&nbsp;<img src="/sys/images/arrow3.gif" style="margin-left:4px;"/>',
                                                       '<input id="ds_cfg_rdoHuntRuleWild" type="radio" value="wild" checked="true" name="huntTarget"/><label for="ds_cfg_rdoHuntRuleWild">野地</label>',
                                                       '<input id="ds_cfg_rdoHuntRuleDevil" type="radio" value="devil" name="huntTarget" /><label for="ds_cfg_rdoHuntRuleDevil">惡魔城</label>',
                                                       '<input id="ds_cfg_rdoHuntRuleWheel" type="radio" value="wheel" name="huntTarget" /><label for="ds_cfg_rdoHuntRuleWheel">水/風車</label>',
                                                  '</td>',
                                              '</tr>',
                                              '<tr>',
                                                  '<td>',
                                                      '进行 &nbsp;&nbsp;<img src="/sys/images/arrow3.gif" style="margin-left:4px;"/>',
                                                      '<input id="ds_cfg_rdoHuntRuleAttack" type="radio" value="attack" checked="true" name="huntType" /><label for="ds_cfg_rdoHuntRuleAttack">攻擊</label>',
                                                      '<input id="ds_cfg_rdoHuntRuleCollect" type="radio" value="collect" name="huntType" /><label for="ds_cfg_rdoHuntRuleCollect">采集</label>',
                                                  '</td>',
                                              '</tr>',
											  '<tr>',
												'<td>',
													'当英雄血少與<input type="radio" name="heroblood" value="50">50%&nbsp;&nbsp;<input type="radio" name="heroblood" value="30">30%<input type="radio" name="heroblood" value="10" checked="true">10%時',
												'</td>',
											  '</tr>',
											  '<tr>',
												'<td>',
													'讓英雄<img src="/sys/images/arrow3.gif" style="margin-left:4px;"/><input type="radio" name="herotodo" value="1" checked="true">繼續出軍&nbsp;&nbsp;<input type="radio" name="herotodo" value="2">暫停出軍&nbsp;&nbsp;<input type="radio" name="herotodo" value="3">到醫療帳篷中<span id="herotodo"></span>',
												'</td>',
											  '</tr>',
                                  '</table>',
                              '</td>',
                          '</tr>',
                          '<tr>',
                              '<td>',
                                  '<div class="ds_addHuntRuleHeroDropArea">請將英雄拖動到本區域中指派狩獵規則</div>',
                              '</td>',
                          '</tr>',
                          '<tr>',
                              '<div><a href="javascript:void(0);" style="margin-left:20px;" class="ds_resetNewHuntRule">重置</a><a href="javascript:void(0);" style="margin-left:20px;" class="ds_addHuntRule">增加編隊</a></div></td>',
                          '</tr>',
                      '</tbody>',
                  '</table>',
              '</form>'
        ].join(''));
		
		$("input[@name='herotodo']").click(function(){
			var todo = $(this).val();
			if(todo=='3'){
				$('#herotodo').html('&nbsp;&nbsp<select name="treatmenttime39893"><option selected="selected" value="1">1 小時</option><option value="2">2 小時</option><option value="3">3 小時</option><option value="5">5 小時</option><option value="8">8 小時</option><option value="12">12 小時</option><option value="24">24 小時</option><option value="48">48 小時</option></select>');
			}else{
				$('select[@name="treatmenttime39893"]').remove();
			}
			
		})

        $('input#ds_chkPointing').click(function(){
            var b = $(this).attr('checked');
            if(b) {
                $("table#ds_change").replaceWith([
                    '<table id="ds_change">',
                        '<tr>',
                              '快捷鏈接名稱：<select id="ds_cfg_attackName"></select>',               
                    '</tr>',
                    '<tr>',
                        '<td>',
                            '進行 &nbsp;&nbsp;<img src="/sys/images/arrow3.gif" style="margin-left:4px;"/>',
                            '<input id="ds_cfg_rdoHuntRuleAttack" type="radio" value="attack" checked="true" name="huntType" /><label for="ds_cfg_rdoHuntRuleAttack">攻擊</label>',
                            '<input id="ds_cfg_rdoHuntRuleSpy" type="radio" value="spy" name="huntType" /><label for="ds_cfg_rdoHuntRuleSpy">偵查</label>',
                        '</td>',
                    '</tr>',
                '</table>'
                ].join(''));
                for (var i = 0 ; i<CONFIG.attackList.length;i++) {
                    var attackCity = CONFIG.attackList[i];
                    $("#ds_cfg_attackName").append("<option>"+attackCity.names+"</option>");
                }
            }else{
                $("table#ds_change").replaceWith([
                    '<table id="ds_change">',
                        '<tr>',
                            '<td>在<input id="ds_cfg_iptHuntRuleRadius" type="text" style="width:20px;" value="22"/>格',
                            '<input type="checkbox" id="ds_cfg_chkHuntRuleFixRadius"/><label for="ds_cfg_chkHuntRuleFixRadius">固定半徑</label>范圍內',                       
                        '</tr>',
                        '<tr>',
                            '<td>',
                                '对于&nbsp;&nbsp;<img src="/sys/images/arrow3.gif" style="margin-left:4px;"/>',
                                '<input id="ds_cfg_rdoHuntRuleWild" type="radio" value="wild" checked="true" name="huntTarget"/><label for="ds_cfg_rdoHuntRuleWild">野地</label>',
                                '<input id="ds_cfg_rdoHuntRuleDevil" type="radio" value="devil" name="huntTarget" /><label for="ds_cfg_rdoHuntRuleDevil">惡魔城</label>',
                                '<input id="ds_cfg_rdoHuntRuleWheel" type="radio" value="wheel" name="huntTarget" /><label for="ds_cfg_rdoHuntRuleWheel">水/風車</label>',
                            '</td>',
                        '</tr>',
                        '<tr>',
                            '<td>',
                                '進行 &nbsp;&nbsp;<img src="/sys/images/arrow3.gif" style="margin-left:4px;"/>',
                                '<input id="ds_cfg_rdoHuntRuleAttack" type="radio" value="attack" checked="true" name="huntType" /><label for="ds_cfg_rdoHuntRuleAttack">攻擊</label>',
                                '<input id="ds_cfg_rdoHuntRuleCollect" type="radio" value="collect" name="huntType" /><label for="ds_cfg_rdoHuntRuleCollect">采集</label>',
                            '</td>',
                        '</tr>',
                    '</table>'
                ].join(''));
            }
        });
        $('a.ds_resetNewHuntRule').click(function(){
            $('input.ds_cfg_iptHuntRuleRadius').val('22');
            $('div.ds_addHuntRuleHeroDropArea').html('請將拖動英雄到本區域中指派狩獵規則');
        });
        $('a.ds_addHuntRule').click(function(){
            var id = new Date().getTime();
            var rule = new Object();
            rule.id = id;
            rule.heros = [];
            $('form.ds_formAddHuntRule > table > tbody > tr > td > div.ds_addHuntRuleHeroDropArea > div.ds_heroItem').each(function(i){
                var hero = {}
                hero.name = $(this).attr('ds_heroname');
                hero.icon = $(this).children(':first').attr('src');
                rule.heros.push(hero);
            })
            if(rule.heros.length == 0){
                alert('請至少選擇一個英雄生成狩獵規則.');
                return;
            }
            //check if exists the same hero in other rules
            for(var i=0;i<rule.heros.length;i++){
                var hero = rule.heros[i];
                for(var j=0;j<CONFIG.hunt_rules.length;j++){
                    var existsrule = CONFIG.hunt_rules[j];
                    if(existsrule.heros.getObjectByProperty('name',hero.name) != null){
                        alert('英雄:'+hero.name+'已經存在于其他規則中,一下英雄只能被分配到一種狩獵規則中.');
                        return;
                    }
                }
            }
            rule.isPointing = $("#ds_chkPointing").get(0).checked;
            if(rule.isPointing){
                rule.attackName = $("#ds_cfg_attackName").val();
                if(rule.attackName == null || rule.attackName == ''){
                    alert('在定點攻擊模式下.您必須選擇一個打擊目標.您可以在敵人城池頁面將該城池加入您的打擊列表.');
                    return;
                }
                rule.attackType = $('input[name="huntType"]:radio:checked').val();
                $("#ds_cfg_attackName").val('');
                $('div.ds_addHuntRuleHeroDropArea').html('請將拖動英雄到本區域中指派狩獵規則');
            }else{
                rule.radius = parseInt($("#ds_cfg_iptHuntRuleRadius").val());
                rule.fixRadius = $("#ds_cfg_chkHuntRuleFixRadius").get(0).checked;
                rule.target = $('input[name="huntTarget"]:radio:checked').val();
                rule.attackType = $('input[name="huntType"]:radio:checked').val();
                $('input.ds_cfg_iptHuntRuleRadius').val('22');
                $('div.ds_addHuntRuleHeroDropArea').html('請將拖動英雄到本區域中指派狩獵規則');
            }
			rule.blood = $("input[name='heroblood']:radio:checked").val();
			rule.herotodo = $('input[name="herotodo"]:radio:checked').val();
			if(rule.herotodo=='3'){rule.Treatment = $('select[@name="treatmenttime39893"]').val();}
            CONFIG.hunt_rules.push(rule);
            //generate ui for rule           
            var form = $('form.ds_formAddHuntRule');
            var html = _generateHuntRuleUI_configMode(rule);
            form.after(html);
            _syncEventForRemoveHuntRule();
        })
        $('div.army_area > ul.hero_info > li > a > img').wrap('<div class="ds_heroAvatar"></div>');
		 $('ul.hero_info > li > a[@class="pic"] > img').wrap('<div class="ds_heroAvatar"></div>');
        $('div.army_area > ul.hero_info > li > a').attr('href','javascript:void(0);');
        $('div.army_area > ul.hero_info > li > a > div.ds_heroAvatar').draggable({helper: 'clone'});
		$('ul.hero_info > li > a[@class="pic"] > div.ds_heroAvatar').draggable({helper: 'clone'});
        $("div.ds_addHuntRuleHeroDropArea").droppable({
            accept: ".ds_heroAvatar",
            activeClass: 'droppable-active',
            hoverClass: 'droppable-hover',
            drop: function(ev, ui) {
                var text = jQuery.trim(ui.draggable.parent().siblings('h3').html());
                if(text != ''){
                    text = jQuery.trim(jQuery.trim(text).split(' ')[1]);
                    if($(this).children('[ds_heroname*="'+text+'"]').length == 0){
                        if($(this).children().length == 0){
                            $(this).html('');
                        }
                        $(this).append('<div class="ds_heroItem" ds_heroname="'+text+'"><img src="'+ui.draggable.children(':first').attr('src')+'" width="32" height="32" title="'+text+'"/></div>');
                    }               
                }
                
            }
        });
        //restore hunt rules data on UI
        var form = $('form.ds_formAddHuntRule');
        for(var i=0;i<CONFIG.hunt_rules.length;i++){
            var html = _generateHuntRuleUI_configMode(CONFIG.hunt_rules[i]);
            form.after(html);
        }
        _syncEventForRemoveHuntRule();     
    }
    
    //code for process config UI of resurrect heros
    if(title.indexOf("酒館")>=0){
        log("進入酒館,您可以在這里進行是否允許自動恢復已經陣亡的英雄的配置.");
        $('div.b_detail > h2 ').append('<span style="margin-left:10px;font-size:14px;">'+getDSIconPath()+'<input type="checkbox" class="ds_cfg_chkAutoResurrectHero" checked />自動復活陣亡的英雄</span>');
        $('div.b_detail > h2 > span > input.ds_cfg_chkAutoResurrectHero').get(0).checked = CONFIG.auto_resurrect;
        $('div.b_detail > h2 > span > input.ds_cfg_chkAutoResurrectHero').click(function(){
            CONFIG.auto_resurrect = this.checked;
        });
    }

}


// private functions             

//generate the hunt rule list UI in config mode
var _generateHuntRuleUI_configMode = function(huntRule){
    var html = ['<form class="ds_formHuntRule$huntruleid">',
          '<table class="t3">',
              '<tbody>',
                  '<tr>',
                      '<td>$heros</td>',                       
                  '</tr>',
                  '<tr>',
                      '<td>',
                          '$detail',
                      '</td>',
                  '</tr>',
                  '<tr>',
                      '<div><a href="javascript:void(0);" style="margin-left:20px;" class="ds_deleteHuntRule" ds_huntruleid="$huntruleid">刪除</a></div></td>',
                  '</tr>',
              '</tbody>',
          '</table>',
      '</form>'].join('');
     html = html.replace(/\$huntruleid/g,huntRule.id);
     var heros = '';
     for(var i=0;i<huntRule.heros.length;i++){
         var objHero = huntRule.heros[i];
         heros += ['<div class="ds_heroItem" ds_heroname="'+objHero.name+'">',
                        '<img height="32" width="32" title="'+objHero.name+'" src="'+objHero.icon+'"/>',
                   '</div>'].join('');
     }
     html = html.replace(/\$heros/g,heros);
     var details = '<img src="/sys/images/arrow3.gif" style="margin-left:4px;"/>';
    if(huntRule.isPointing){
        if(huntRule.attackType == "attack"){
            details += "<strong style='margin-right:4px;margin-left:4px;'>定點攻擊</strong>"
        }else if (huntRule.attackType == "spy"){
            details += "<strong style='margin-right:4px;margin-left:4px;'>定點偵查</strong>"
        }
        details += "<strong style='margin-right:4px;margin-left:4px;'>快捷鏈接名为：" + huntRule.attackName.toString() + "的地方</strong>"
    }else{
        if(huntRule.attackType == "attack"){
                details += "<strong style='margin-right:4px;margin-left:4px;'>攻擊</strong>"
        }else{
                details += "<strong style='margin-right:4px;margin-left:4px;'>采集</strong>"
        }
        details += "<strong style='margin-right:4px;margin-left:4px;'>" + huntRule.radius.toString() + "格</strong>"
        if(huntRule.fixRadius == true){
            details += "<strong style='margin-right:4px;margin-left:4px;'>固定半徑</strong>上"
        }else{
            details += "<strong style='margin-right:4px;margin-left:4px;'>隨機半徑</strong>内"
        }
        details += '的';
        if(huntRule.target == "wild")  details += "野地";
        else if(huntRule.target == "devil") details += "惡魔城";
        else details += "水/風車";

		details +="&nbsp;&nbsp;<strong style='margin-right:4px;margin-left:4px;'>當血量少與"+huntRule.blood+"% 時";
		if(huntRule.herotodo=='1'){
			details +="繼續出軍";
		}else if(huntRule.herotodo=='2'){
			details +="暫停出軍";
		}else{
			details +="到醫療帳篷中恢復"+huntRule.Treatment+"小時</strong>";
		}
    }
    html = html.replace(/\$detail/g,details);
    return html;
}

var _syncEventForRemoveHuntRule = function(){
    $('a.ds_deleteHuntRule').click(function(){
       var id = $(this).attr('ds_huntruleid');
       $('form.ds_formHuntRule'+id).remove();
       CONFIG.hunt_rules.removeByObjectProperty('id',id);
    });
}

//generate the hunt rule list UI in normal mode
var _generateHuntRuleUI = function(huntRule){
    var html = ['<form class="ds_formHuntRule$huntruleid">',
          '<table class="t3">',
              '<tbody>',
                  '<tr>',
                      '<td>$heros</td>',                       
                  '</tr>',
                  '<tr>',
                      '<td>',
                          '$detail',
                      '</td>',
                  '</tr>',
              '</tbody>',
          '</table>',
      '</form>'].join('');
     html = html.replace(/\$huntruleid/g,huntRule.id);
     var heros = '';
     for(var i=0;i<huntRule.heros.length;i++){
         var objHero = huntRule.heros[i];
         heros += ['<div class="ds_heroItem" ds_heroname="'+objHero.name+'">',
                        '<img height="32" width="32" title="'+objHero.name+'" src="'+objHero.icon+'"/>',
                   '</div>'].join('');
     }
     html = html.replace(/\$heros/g,heros);
     var details = '<img src="/sys/images/arrow3.gif" style="margin-left:4px;"/>';
    if  (huntRule.isPointing) {
                if(huntRule.attackType == "attack"){
                        details += "<strong style='margin-right:4px;margin-left:4px;'>定點攻擊</strong>"
                }else if (huntRule.attackType == "spy"){
                        details += "<strong style='margin-right:4px;margin-left:4px;'>定點偵查</strong>"
                }
                details += "<strong style='margin-right:4px;margin-left:4px;'>快捷鏈接名为：" + huntRule.attackName.toString() + "的地方</strong>"
    } else {
                if(huntRule.attackType == "attack"){
                        details += "<strong style='margin-right:4px;margin-left:4px;'>攻擊</strong>"
                }else {
                        details += "<strong style='margin-right:4px;margin-left:4px;'>采集</strong>"
                }
                details += "<strong style='margin-right:4px;margin-left:4px;'>" + huntRule.radius.toString() + "格</strong>"
                if(huntRule.fixRadius == true){
                        details += "<strong style='margin-right:4px;margin-left:4px;'>固定半徑</strong>上"
                }else{
                        details += "<strong style='margin-right:4px;margin-left:4px;'>隨機半徑</strong>内"
                }
                details += '的';
                if(huntRule.target == "wild")  details += "野地";
                else if(huntRule.target == "devil") details += "惡魔城";
                else details += "水/風車";

				details +="&nbsp;&nbsp;<strong style='margin-right:4px;margin-left:4px;'>當血量少與"+huntRule.blood+"% 時";
				if(huntRule.herotodo=='1'){
					details +="繼續出軍";
				}else if(huntRule.herotodo=='2'){
					details +="暫停出軍";
				}else{
					details +="到醫療帳篷中恢復"+huntRule.Treatment+"小時</strong>";
				}
    }
     html = html.replace(/\$detail/g,details);
     return html;
}
